/*
 * Rectangle.cpp
 *
 *      Author: Jerome RD Soine and Christoph A Brand
 *      Institution: Schwarz goup, Institute for Theoretical Physics, Heidelberg University, Germany
 */

#include "../../../include/core/shape/Rectangle.h"
#include <cmath>
#include <fstream>
#include <iostream>
#include <stdlib.h>

using namespace std;

bool Rectangle::is_on_shape(vector<double> point)
{
	vector<double> rotated_pt = rotate(point,orientation_angle);
	vector<double> rotated_center = rotate(center,orientation_angle);
	if(abs(rotated_pt[0]-rotated_center[0])<=a*0.5 && abs(rotated_pt[1]-rotated_center[1])<=b*0.5)
		return true;
	return false;
}


vector<double> Rectangle::rotate(vector<double> &point, double angle)
{
	vector<double> rotated_point (2);
	rotated_point[0] = point[0]*cos(angle) - point[1]*sin(angle);
	rotated_point[1] = point[0]*sin(angle) - point[1]*cos(angle);
	return rotated_point;
}

vector<Shape*> Rectangle::get_shapes_from_file(string FILENAME)
{
	vector<double> x_pos;
	vector<double> y_pos;
	vector<double> angle;
	vector<double> major;
	vector<double> minor;

	double tmp;

	ifstream input (FILENAME.c_str());
	if(input.good())
	{
		while(!input.eof())
		{
				input >> tmp; x_pos.push_back(tmp);
				input >> tmp; y_pos.push_back(tmp);
				input >> tmp; angle.push_back(tmp);
				input >> tmp; major.push_back(tmp);
				input >> tmp; minor.push_back(tmp);
		}
		input.close();
	}
	else
	{
		cout << "ERROR: with file " << FILENAME << " -> no such file or directory." << endl;
		exit(1);
	}

	vector<Shape*> rectangles;

	for(unsigned int i=0;i<x_pos.size();i++)
	{
		vector<double> pos (2,0);
		pos [0] = x_pos[i];
		pos [1] = y_pos[i];
		rectangles.push_back(new Rectangle(pos,flip_orientation(angle[i]),major[i],minor[i]));
	}

	return rectangles;
}

double Rectangle::flip_orientation(double angle)
{
	double pi = 3.14159265359;
	return abs(angle-pi);
}

